﻿<?php
	class Citydevelop{
	    
		private $id;
		private $name; 
		private $time;
		private $star;
		private $label;
		private $library;
		private $conn;
		
		function __construct(){
			$this->conn = conn();
		}	

		public function getYear($date){
			 $str = explode('-',$date);			 
			 return $str[0];
		}
		
		public function getDateByYear($year){
			$date = $year."-01-01";
			return $date;
		}	
		
		public function listAll(){
			$query = "select * from Cases";
			$stmt = $this->conn->query( $query ); 
			$items = array();
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				$id = $row['id'];
				$name = $row['name'];
				$time = $row['starttime'];
				$time = $this->getYear($time);
				$star = $row['star'];
				$label = $row['label'];
				$library = $row['library'];
				$item=array("id"=>$id,"name"=>$name,"time"=>$time,"star"=>$star,
					"label"=>$label,"library"=>$library);
				array_push($items, $item);
			}	
			var_dump($items);
			return $items;
		}

		public function listAll1(){
			$query = "select * from Cases where library = '城市发展案例库'";
			$stmt = $this->conn->query( $query ); 
			$items = array();
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				$id = $row['id'];
				$name = $row['name'];
				$time = $row['starttime'];
				$time = $this->getYear($time);
				$star = $row['star'];
				$label = $row['label'];
				$library = $row['library'];
				$item=array("id"=>$id,"name"=>$name,"time"=>$time,"star"=>$star,
					"label"=>$label,"library"=>$library);
				array_push($items, $item);
			}	
			var_dump($items);
			return $items;
		}		

		public function listAll2(){
			$query = "select * from Cases where library ='城市百科知识库'";
			$stmt = $this->conn->query( $query ); 
			$items = array();
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				$id = $row['id'];
				$name = $row['name'];
				$time = $row['starttime'];
				$time = $this->getYear($time);
				$star = $row['star'];
				$label = $row['label'];
				$library = $row['library'];
				$item=array("id"=>$id,"name"=>$name,"time"=>$time,"star"=>$star,
					"label"=>$label,"library"=>$library);
				array_push($items, $item);
			}	
			var_dump($items);
			return $items;
		}		
		
		public function listAll3(){
			$query = "select * from Cases where library = '城市数据知识库'";
			$stmt = $this->conn->query( $query ); 
			$items = array();
			while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ) { 
				$id = $row['id'];
				$name = $row['name'];
				$time = $row['starttime'];
				$time = $this->getYear($time);
				$star = $row['star'];
				$label = $row['label'];
				$library = $row['library'];
				$item=array("id"=>$id,"name"=>$name,"time"=>$time,"star"=>$star,
					"label"=>$label,"library"=>$library);
				array_push($items, $item);
			}	
			var_dump($items);
			return $items;
		}
		
		public function update($item){
		
			$file = $item['file'];
			//dir 默认./Doc/anlipouxi/
			$this->upload($file);
			$pinfo=pathinfo($file["name"]);
			$filename=$pinfo['basename'];//文件名
			$dir = "./Doc/anlipouxi/".$filename.".pdf";
		
			$id = $item['id'];
			$name = $item['name'];
			$time = $item['time'];
			$time = $this->getDateByYear($time);
			$star = $item['star'];
			$label = $item['label'];
			$library = $item['library'];
			//$dir = 'ssss';
			//$dir = $item['email'];
			$query = "update Cases SET name ='".$name."', starttime = '".$time."', star = '".$star."'
				, label = '".$label."', library='".$library."', dir='".$dir."' where id = '".$id."'";
			//var_dump( $query);
			
			$stmt = $this->conn->query( $query );
		}		
		
		public function upload($file){

			if(!is_dir('Doc'))
			{
			  mkdir('Doc');//路径若不存在则创建
			}
			$path = "./Doc/anlipouxi/";
			$filepath = $path.$file['name']."pdf";
			$tmp_name = $file['tmp_name'];
			$showphpath = $path.$file['name'];
			
			$filepath = iconv("utf-8","gbk",$filepath);
			$showphpath = iconv("utf-8","gbk",$showphpath);
			var_dump($filepath);
			if($file['type'] != 'application/pdf')
			{
			   echo "文件类型不符pdf格式！";
			   die();
			}
			
			if(file_exists ($filepath))
			{
				unlink($filepath);
				echo "删除已存在的同名文件!";
			}
			
			if(move_uploaded_file($tmp_name,$showphpath))
				echo "上传成功！";		
			else
			{
				echo "文件上传失败！";
				die();
			}		
		}
		
		public function insert($item){

			$name = $item['name'];
			$time = $item['time'];
			$time = $this->getDateByYear($time);
			$star = $item['star'];
			$label = $item['label'];
			$library = $item['library'];
			//$dir = $item['dir'];
			
			$file = $item['file'];
			//dir 默认./Doc/anlipouxi/
			$this->upload($file);
			$pinfo=pathinfo($file["name"]);
			$filename=$pinfo['basename'];//文件名
			$dir = "./Doc/anlipouxi/".$filename.".pdf";
			
			$query = "INSERT INTO Cases(name,starttime,star,label,library,dir)
			VALUES('".$name."','".$time."','".$star."','".$label."','".$library."','".$dir."')";
			var_dump($query);
			//die();
			$stmt = $this->conn->query( $query );
		}	
		
		public function delete($id){
			 $query = "delete from Cases where id = '".$id."'";
			 $stmt = $this->conn->query( $query );
		}
		
		public function getById($id){
			$query = "select * from Cases where id = '".$id."'";
			$stmt = $this->conn->query( $query );
			$row = $stmt->fetch( PDO::FETCH_ASSOC );
			$id = $row['id'];
			$name = $row['name'];
			$time = $row['starttime'];
			$time = $this->getYear($time);
			$star = $row['star'];
			$label = $row['label'];
			$library = $row['library'];
			$dir = $row['dir'];
			$item=array("id"=>$id,"name"=>$name,"time"=>$time,"star"=>$star,
				"label"=>$label,"library"=>$library,"dir"=>$dir);
			return $item;			
		}
		
	}
?>